package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: RoleController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/7/27 21:52
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /*
    1.角色列表查询
     */
    @GetMapping
    public Result list(String name, String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("条件分页查询角色：name={}, label={}, page={}, pageSize={}", name, label, page, pageSize);

        //调用service，分页查询
        PageResult<Role> pageResult = roleService.listRoles(name, label, page, pageSize);
        return Result.success(pageResult);
    }

    /*
    2.删除角色
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色：{}", id);
        roleService.delete(id);
        return Result.success();
    }

    /*
    3.添加角色
    @RequestBody: 将请求体中json数据封装为对象
     */
    @PostMapping
    public Result add(@RequestBody Role role) {
        log.info("添加角色{}" , role);
        roleService.add(role);
        return Result.success();
    }

    /*
    4.根据id查询
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("根据id查询角色:{}", id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    /*
    5.修改部门
     */
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改部门: {}" , role);
        roleService.update(role);
        return Result.success();
    }

    /*
    6.查询所有角色
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有角色");
        List<Role> list = roleService.list();
        return Result.success(list);
    }


}
